package com.android.support.lib.universal.util;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.base.BaseApplication;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 动态设置控件大小
 * 
 * UtilView.setViewParam(getActivity(), img_stra, 0, 7);// 动态设置控件的高度
 * 
 * @author 彭治铭
 *
 */
public class Utilview {

	/**
	 * 动态设置控件大小
	 * 
	 * @param activity
	 *            当前Activity上下文
	 * @param view
	 *            控件（可以是任意控件）
	 * @param width_proportion
	 *            宽的比例（相对于整个屏幕的宽度），小于或等于0不设置。1就是 1/1 ,2就是1/2 ,3就是 1/3
	 * @param height_proportion
	 *            高的比例 ，同上。
	 * @param bool
	 *            true,宽与宽作对比，高与高作对比;false,互换。宽与高作对比
	 */
	public static void setViewParam(Activity activity, View view,
			float width_proportion, float height_proportion, Boolean bool) {
		try {
			DisplayMetrics dm = new DisplayMetrics();
			activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

			int width = dm.widthPixels;
			int height = dm.heightPixels;

			LayoutParams laParams = view
					.getLayoutParams();
			if (width_proportion > 0) {
				if (bool) {
					laParams.width = (int) (width / width_proportion);
				} else {
					laParams.width = (int) (height / width_proportion);
				}

			}
			if (height_proportion > 0) {
				if (bool) {
					laParams.height = (int) (height / height_proportion);
				} else {
					laParams.height = (int) (width / height_proportion);
				}

			}

			view.setLayoutParams(laParams);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("动态控制控件大小异常", "原因" + e.getMessage());
		}

	}

	/**
	 * 动态设置控件大小，父容器，和子容器可以为同一个容器，自己对照自己。
	 * 
	 * @param parent
	 *            父容器，即相对控件。任意控件即可
	 * @param child
	 *            子容器，即当前控件
	 * @param w
	 *            子容器相对于父容器的宽,参数大于0才有效,0~1是缩小，大于1则是放大
	 * @param h
	 *            子容器相对于父容器的高，参数大于0才有效
	 */
	public static void setViewParam(View parent, View child, float w, float h) {

		try {
			int width = parent.getLayoutParams().width;
			int height = parent.getLayoutParams().height;
			LayoutParams laParams = child
					.getLayoutParams();
			if (w > 0) {
				laParams.width = (int) (width * w);
			}
			if (h > 0) {
				laParams.height = (int) (height * h);
			}

			child.setLayoutParams(laParams);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("动态控制控件大小异常2", "原因" + e.getMessage());
		}

	}

	/**
	 * 动态设置控件大小,自己的宽和高做对比
	 * 
	 * @param view
	 *            控件
	 * @param w
	 *            宽是高的几倍，参数大于0才有效,0~1是缩小，大于1则是放大
	 * @param h
	 *            高是宽的几倍
	 */
	public static void setViewParam(View view, float w, float h) {
		try {
			int width = view.getLayoutParams().width;
			int height = view.getLayoutParams().height;

			LayoutParams laParams = view
					.getLayoutParams();
			if (w > 0) {
				laParams.width = (int) (height * w);
			}
			if (h > 0) {
				laParams.height = (int) (width * h);
			}

			view.setLayoutParams(laParams);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("动态控制控件大小异常3", "原因" + e.getMessage());
		}
	}

	/**
	 * 直接设置控件的宽 和 高。
	 * 
	 * @param view
	 *            控件
	 * @param width
	 *            具体的宽度，大于0才有效
	 * @param height
	 *            具体的高度，大于0才有效
	 */
	public static void setViewParam(View view, int width, int height) {
		LayoutParams laParams = view.getLayoutParams();
		if (width > 0) {
			laParams.width = width;
		}
		if (height > 0) {
			laParams.height = height;
		}
		view.setLayoutParams(laParams);
	}

	/**
	 * 利用外补丁，设置控件的位置
	 * 
	 * @param context
	 *            上下文
	 * @param view
	 *            控件
	 * @param left
	 *            左外补丁（相对于整个屏幕，取百分比。0~1获取-1~0之间，1代表100%）,其他数值则为具体的数值。以下同上。
	 * @param top
	 *            上外补丁
	 * @param right
	 *            右外补丁
	 * @param bottom
	 *            下外补丁
	 * @param Linearlayout
	 *            父容器为线性布局时传人父容器对象，如果为null,父容器会被当做相对布局处理，且以下两个参数无效。
	 * @param HORIZONTAL
	 *            左右对齐方式，Gravity.LEFT，Gravity.RIGHT，Gravity.CENTER_HORIZONTAL
	 * @param VERTICAL
	 *            垂直对齐方式，Gravity.TOP，Gravity.BOTTOM，Gravity.CENTER_VERTICAL
	 */
	public static void setViewMargin(Context context, View view, float left,
			float top, float right, float bottom, LinearLayout Linearlayout,
			int HORIZONTAL, int VERTICAL) {
		Utilscreen utilscreen = new Utilscreen(context);
		int width = utilscreen.getScreenWidth();
		int height = utilscreen.getScreenHeight()-utilscreen.getStatusHeight();
		MarginLayoutParams margin = new MarginLayoutParams(
				view.getLayoutParams());

		if ((left >= 0 && left <= 1) || (left >= -1 && left <= 0)) {
			margin.leftMargin = (int) (width * left);
		} else {
			margin.leftMargin = (int) left;
		}

		if ((top >= 0 && top <= 1) || (top >= -1 && top <= 0)) {
			margin.topMargin = (int) (height * top);
		} else {
			margin.topMargin = (int) top;
		}

		if ((right >= 0 && right <= 1) || (right >= -1 && right <= 0)) {
			margin.rightMargin = (int) (width * right);
		} else {
			margin.rightMargin = (int) right;
		}

		if ((bottom >= 0 && bottom <= 1) || (bottom >= -1 && bottom <= 0)) {
			margin.bottomMargin = (int) (height * bottom);
		} else {
			margin.bottomMargin = (int) bottom;
		}

		// 把新的边距生成layoutParams对象
		if (Linearlayout != null) {
			// 设置线性布局的Gravity对齐方式
			Linearlayout.setGravity(HORIZONTAL | VERTICAL);
			LinearLayout.LayoutParams layoutParamsMargin = new LinearLayout.LayoutParams(
					margin);
			// 设制view的新的位置
			view.setLayoutParams(layoutParamsMargin);

		} else {
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
					margin);
			// 设制view的新的位置
			view.setLayoutParams(layoutParams);
		}
		utilscreen = null;
	}

	/**
	 * 获取自定义View
	 * 
	 * @param context
	 * @param layoutId
	 *            布局ID
	 * @return
	 */
	public static View getView(Context context, int layoutId) {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater.inflate(layoutId, null);
		return view;
	}

	/**
	 * 显示自定义Toast View
	 *
	 * @param text
	 */
	public static void showToastView( String text) {
		Toast toast = new Toast(BaseApplication.getInstance());
		toast.setGravity(Gravity.CENTER, 0, 0);// 显示位置
		toast.setDuration(Toast.LENGTH_LONG);// 显示时长，1000为1秒
		View view = getView(BaseApplication.getInstance(), R.layout.lib_toast);
		TextView textView = (TextView) view.findViewById(R.id.lib_tost_text);
		textView.setText(text);
		view.getBackground().setAlpha(100);// 设置透明的
		toast.setView(view);// 自定义view
		toast.show();
	}

}
